/**
 * Copyright (C) 2012 Oleg Anastassov <o.anastasov@gmail.com>
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package kz.kstu.ent.desktop.ui.dialog;

import kz.kstu.ent.desktop.images.GUIFactory;
import kz.kstu.ent.desktop.images.GUIIcon;
import kz.kstu.ent.desktop.ui.ControllerLife;
import kz.kstu.ent.desktop.ui.DialogController;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class CommonDialog extends JDialog implements ControllerLife {

    public static final int WIDTH = 480;
    public static final int HEIGHT = 520;

    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JPanel mainPanel;

    private int width;
    private int height;

    private DialogController dialogController;

    public CommonDialog(DialogController dialogController) {
        this.dialogController = dialogController;
        this.width = WIDTH;
        this.height = HEIGHT;
    }

    public CommonDialog(DialogController dialogController, int width, int height) {
        this.dialogController = dialogController;
        this.width = width;
        this.height = height;
    }

    @Override
    public void initStaticComponents() {
        setContentPane(contentPane);
        setModal(true);
        setResizable(false);
        setSize(width, height);
        setTitle(dialogController.getTitle());
        getRootPane().setDefaultButton(buttonOK);
        initIcons();
        initListeners();
        mainPanel.add(dialogController.getJComponent(), BorderLayout.CENTER);
        dialogController.initStaticComponents();
    }

    private void initIcons() {
        setIconImage(
                GUIFactory.getImage(GUIIcon.TRANSPORTATION)
        );
        buttonOK.setIcon(
                GUIFactory.getIcon(GUIIcon.OK)
        );
        buttonCancel.setIcon(
                GUIFactory.getIcon(GUIIcon.CANCEL)
        );
    }

    private void initListeners() {
        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOk();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

    }

    @Override
    public void initData() {
        dialogController.initData();
    }

    public void onOk() {
        if (dialogController.accept()) {
            dispose();
        }
    }

    public void onCancel() {
        dialogController.decline();
        dispose();
    }
}
